﻿////////////////////////////////////////////////////////////////////////
//                                                                    //
//                 Project: MediaElement Extensions                   //
//               Project URL: http://mext.codeplex.com                //
//                           GPL License v2                           //
//                                                                    //
//             Written by Geoffrey DANIEL { NeuroCypher }             //
//           Blog: http://blogs.developpeur.org/neurocypher           //
//                                                                    //
////////////////////////////////////////////////////////////////////////

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using MediaElementExtensions.Controls.Bases;
using MediaElementExtensions.Controls.Interfaces;

namespace MediaElementExtensions.Controls
{

    public class VolumeSlider : SliderMediaElementExtensionControlBase, ISoundMediaElementControl
    {

        #region Dependency Properties

        #region AutoDisableWhenMuted

        public static DependencyProperty AutoDisableWhenMutedProperty =
            DependencyProperty.Register("AutoDisableWhenMuted",
                                        typeof(bool),
                                        typeof(VolumeSlider),
                                        new PropertyMetadata(new PropertyChangedCallback(
                                            (o, e) =>
                                            {
                                                var ctrl = o as VolumeSlider;

                                                if (ctrl != null)
                                                {
                                                    ctrl.CheckIsEnabled();
                                                }
                                            })));

        public bool AutoDisableWhenMuted
        {
            get { return (bool)GetValue(AutoDisableWhenMutedProperty); }
            set { SetValue(AutoDisableWhenMutedProperty, value); }
        }

        #endregion

        #endregion

        #region Properties

        // Some properties must be bridled by the style
        // in order to correctly work [ because of the code behind ]
        #region Bridling

        public new Orientation Orientation
        {
            get { return base.Orientation; }
            private set { base.Orientation = value; }
        }

        public new double Minimum
        {
            get { return base.Minimum; }
            private set { base.Minimum = value; }
        }

        public new double Maximum
        {
            get { return base.Maximum; }
            private set { base.Maximum = value; }
        }

        public new double Value
        {
            get { return base.Value; }
            private set { base.Value = value; }
        }

        #endregion

        #endregion

        #region Constructor

        public VolumeSlider()
        {

            InitializeActions(
                CheckIsEnabled,
                (s, e) => CheckIsEnabled(),
                null,
                null,
                (s, e) => CheckIsEnabled(),
                (s, e) => CheckIsEnabled());

            CheckIsEnabled();

            DefaultStyleKey = typeof(VolumeSlider);

        }

        #endregion

        #region Bindings' Management

        public override void InitializeBindings()
        {

            var binding = new Binding();
            binding.Mode = BindingMode.TwoWay;
            binding.Source = this;
            binding.Path = new PropertyPath("MediaElement.Volume");

            SetBinding(ValueProperty, binding);

        }

        #endregion

        #region Actions

        public override void CheckIsEnabled()
        {

            if (AutoDisableWhenNoSourceLoaded == true &&
                (MediaElement == null || MediaElement.CurrentState == MediaElementState.Closed))
            {
                IsEnabled = false;
            }
            else if (AutoDisableWhenMuted == true && MediaElement.IsMuted == true)
            {
                IsEnabled = false;
            }
            else
            {
                IsEnabled = true;
            }

        }

        #endregion

    }

}
